<?php
$id = null;
if(isset($_REQUEST['id']) && trim($_REQUEST['id']) != '') {
	$id = $_REQUEST['id'];
}
$isNewMode = false;
if ($id === null) {
	$isNewMode = true;
}
$category = null;
if(!$isNewMode) { // update mode
	$cateDao = new Dao_CategoryDAO();
	$category = $cateDao->getCategorybyId($id);
}

if ($category === null && !$isNewMode) {
	echo "category is not found";
} else if($category !== null && !$isNewMode) {
	// update category
} else {
  // new mode
	$category = new Model_Category(array());
	$category->setOrderIndex(0);
}

?>
<script type="text/javascript">
Ext.onReady(function(){
	var states = Ext.create('Ext.data.Store', {
	    fields: ['value', 'dis'],
	    data : [
	        {"value":"0", "dis":"Đang dùng"},
	        {"value":"1", "dis":"Đã xóa"}
	    ]
	});

	// Create the combo box, attached to the states data store
	Ext.create('Ext.form.ComboBox', {
	    fieldLabel: ' ',
	    store: states,
	    queryMode: 'local',
	    displayField: 'dis',
	    valueField: 'value',
	    hideLabel : true,
	    height : 22,
	    width  : 120,
	    value:"0",
	    itemId : "cmbStausCate",
	    id:"cmbStausCate",
	    renderTo: "ComboCateStatusPlace"
	});

	Ext.create('Ext.form.field.Number', {
		itemId : "txtOrderIndex",
		id:"txtOrderIndex",
	    hideLabel : true,
	    width : 120,
	    height : 22,
	    value :0,
	    minValue : 0,
	    allowBlank : false,
	    blankText : "Phải&nbsp;nhập&nbsp;thứ&nbsp;tự&nbsp;hiển&nbsp;thị",
	    renderTo: "txtOrderIndexCatePlace"
	});

	Ext.create('Ext.form.field.Text', {
		itemId : "txtNameCate",
		id:"txtNameCate",
	    hideLabel : true,
	    width : 220,
	    height : 22,
	    renderTo: "txtNameCatePlace",
	    allowBlank : false,
	    blankText : "Phải&nbsp;nhập&nbsp;tên",
	    maxLength : 50,
	    enforceMaxLength : true
	});

	var name = $('#name').val();
	var orderIndex = $('#orderIndex').val();
	var delFlg = $('#deleteFLg').val();

	var txtNameCate = Ext.getCmp("txtNameCate");
	var txtOrderIndex = Ext.getCmp("txtOrderIndex");
	var cmbStausCate = Ext.getCmp("cmbStausCate");

	txtNameCate.setValue(name);
	txtOrderIndex.setValue(orderIndex);
	cmbStausCate.setValue(delFlg);
});

function submitForm() {
	var el = Ext.getCmp("txtNameCate");
	if (!el.isValid()) {
		showError("Phải nhập tên", el);
		return false;
	}

	el = Ext.getCmp("txtOrderIndex");
	if (!el.isValid()) {
		showError("Phải nhập thứ tự hiển thị", el);
		return false;
	}

	var name = Ext.getCmp("txtNameCate").getValue();
	var orderIndex = Ext.getCmp("txtOrderIndex").getValue();
	var delFlg = Ext.getCmp("cmbStausCate").getValue();

	$('#name').val(name);
	$('#orderIndex').val(orderIndex);
	$('#deleteFLg').val(delFlg);

	$('#form1').submit();
}

</script>
<form action="process/processcategory.php" method="post" id="form1">
	<input type="hidden"  name="id" value="<?php echo $category->getId();?>">
	<input type="hidden" id="name" name="name" value="<?php echo $category->getName();//echoEscapeStr($category->getName()); ?>">
	<input type="hidden" id="orderIndex" name="orderIndex" value="<?php echoEscapeStr($category->getOrderIndex()); ?>">
	<input type="hidden" id="deleteFLg" name="deleteFLg" value="<?php echoEscapeStr($category->getDeleteFlg()); ?>">
	<table class="table-list" id="tableForm" >
		<tr>
			<td colspan="2" class="header-edit">Thêm / chỉnh sửa category
			<?php  if($category !== null && !$isNewMode) {?>
			(cập nhật)
			<?php } else { ?>
			(Thêm mới)
			<?php } ?>
			</td>
		</tr>
		<?php  if($category !== null && !$isNewMode) {?>
		<tr height="40px;">
			<td width="15%">Id</td>
			<td>
			<?php echo $category->getId(); ?>
		</td>
		<?php } ?>
		</tr>
		<tr height="50px;">
			<td width="15%">Tên</td>
			<td >
				<span id="txtNameCatePlace"></span>(Tối đa 50 ký tự)
			</td>
		</tr>
		<tr height="50px;">
			<td width="15%">Thứ tự hiển thị</td>
			<td>
				<span id="txtOrderIndexCatePlace"></span> (Chỉ nhập số)
			</td>
		</tr>

		<tr height="50px;">
			<td width="15%">Trạng thái sử dụng</td>
			<td>
				<span id="ComboCateStatusPlace"></span>
			</td>
		</tr>
		<tr>
			<td colspan="2" style="text-align: right;">
			<!-- <input type="button" value="Xóa"> -->
			<input type="button" value="Lưu" onclick="submitForm();">

			<?php
				$url = getUrlAdmin('listcategory.php', null);
			?>
			<input type="button" value="quay lại"
				onclick="location.href='<?php echo $url;?>'"></td>
		</tr>
	</table>
</form>
